//
//  TJTitleCellButton.m
//  BaoDian
//
//  Created by mac on 2024/7/22.
//

#import "TJTitleCellButton.h"
 

@implementation TJTitleCellButton {
    UIImageView *__weak _arrowImageView;
    UIView *__weak _lineView;
    UILabel *__weak _titleLabel;
    UILabel *__weak _detailTitleLabel;

}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self settingSubView];
    }
    return self;
}

- (void)settingSubView {
 
    _titleLabel = [UILabel lableWithSuperView:self font:UIFontMake(14) color:UIColor.whiteColor title:nil textAlignment:NSTextAlignmentLeft];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.offset(0);
    }];
 
    
    _arrowImageView = [UIImageView addToView:self];
    _arrowImageView.image = [UIImage imageNamed:@"cell_arrow"];
    [_arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(15);
        make.centerY.offset(0);
    }];
    
    _lineView = [UIView addToView:self];
    _lineView.backgroundColor = [UIColor.tj_CCCCCCColor colorWithAlphaComponent:0.2];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(0);
        make.height.offset(lineHeight);
    }];
    
    _detailTitleLabel = [UILabel lableWithSuperView:self font:UIFontMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentRight];
    [_detailTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_arrowImageView.mas_left).inset(3);
        make.centerY.offset(0);
        make.width.mas_lessThanOrEqualTo(200);
    }];
 
}

- (void)setModel:(TJCellModel *)model {
    _model = model;

    _titleLabel.text = model.text;
    _detailTitleLabel.text = model.detailText;
    _lineView.hidden = !model.showLine;
    
    if (model.showArrow) {
        _arrowImageView.hidden = NO;
        [_arrowImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.inset(15);
            make.centerY.offset(0);
        }];
        
        [_detailTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_arrowImageView.mas_left).inset(5);
            make.centerY.offset(0);
            make.width.mas_lessThanOrEqualTo(200);
        }];
 
    } else {
        _arrowImageView.hidden = YES;
        [_arrowImageView mas_remakeConstraints:^(MASConstraintMaker *make) {}];
        
        [_detailTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.inset(15);
            make.centerY.offset(0);
            make.width.mas_lessThanOrEqualTo(200);
        }];
    }
    
    [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.inset(model.separatorInset.left);
        make.right.inset(model.separatorInset.right);
    }];
    
    _titleLabel.font = model.textFont ?: UIFontMake(14);
    _titleLabel.textColor = model.textColor ?: UIColor.whiteColor;

    _detailTitleLabel.font = model.detailTextFont ?: UIFontMake(14);
    _detailTitleLabel.textColor = model.detailTextColor ?: UIColor.tj_9D9D9DColor;
}
 
- (void)reloadData {
    self.model = _model;
}

@end

